Load all required libraries.

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ------------------------------------------------------------------------ tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.0
## v tidyr   1.1.0     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts --------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
        p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 289)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 12.92260 12.92441 12.92624 12.92810 12.92997 12.93183 12.93368 12.93551
##   [9] 12.93731 12.93907 12.94076 12.94240 12.94396 12.94543 12.94680 12.94807
##  [17] 12.94921 12.95023 12.95111 12.95183 12.95240 12.95279 12.95304 12.95318
##  [25] 12.95323 12.95318 12.95304 12.95281 12.95250 12.95212 12.95166 12.95114
##  [33] 12.95056 12.94992 12.94923 12.94850 12.94772 12.94690 12.94605 12.94518
##  [41] 12.94428 12.94336 12.94243 12.94149 12.94055 12.93961 12.93867 12.93775
##  [49] 12.93684 12.93595 12.93499 12.93389 12.93265 12.93128 12.92978 12.92819
##  [57] 12.92649 12.92470 12.92284 12.92091 12.91892 12.91688 12.91481 12.91271
##  [65] 12.91060 12.90848 12.90636 12.90426 12.90218 12.90014 12.89814 12.89620
##  [73] 12.89432 12.89252 12.89081 12.88919 12.88768 12.88628 12.88453 12.88198
##  [81] 12.87869 12.87472 12.87012 12.86497 12.85931 12.85321 12.84673 12.83993
##  [89] 12.83286 12.82559 12.81817 12.81067 12.80314 12.79564 12.78824 12.78099
##  [97] 12.77395 12.76718 12.76075 12.75471 12.74911 12.74403 12.73951 12.73562
## [105] 12.73242 12.72997 12.72769 12.72499 12.72193 12.71857 12.71496 12.71115
## [113] 12.70720 12.70316 12.69909 12.69505 12.69108 12.68725 12.68361 12.68020
## [121] 12.67710 12.67434 12.67199 12.67011 12.66873 12.66793 12.66776 12.66826
## [129] 12.66950 12.67153 12.67440 12.67817 12.68290 12.68938 12.69817 12.70894
## [137] 12.72135 12.73508 12.74981 12.76519 12.78090 12.79662 12.81201 12.82674
## [145] 12.84048 12.85291 12.86369 12.87435 12.88650 12.90002 12.91474 12.93054
## [153] 12.94726 12.96476 12.98289 13.00151 13.02047 13.03964 13.05885 13.07798
## [161] 13.09687 13.11538 13.13336 13.15068 13.16718 13.18271 13.19714 13.21032
## [169] 13.22211 13.23236 13.24263 13.25446 13.26769 13.28213 13.29761 13.31394
## [177] 13.33095 13.34846 13.36628 13.38426 13.40219 13.41992 13.43725 13.45401
## [185] 13.47003 13.48512 13.49910 13.51181 13.52305 13.53265 13.54044 13.54623
## [193] 13.54985 13.55112 13.54986 13.54590 13.53936 13.53067 13.52005 13.50774
## [201] 13.49396 13.47895 13.46294 13.44615 13.42882 13.41117 13.39344 13.37587
## [209] 13.35867 13.34208 13.32391 13.30215 13.27729 13.24983 13.22028 13.18912
## [217] 13.15686 13.12398 13.09100 13.05840 13.02668 12.99634 12.96788 12.94179
## [225] 12.91523 12.88540 12.85287 12.81821 12.78199 12.74479 12.70718 12.66972
## [233] 12.63299 12.59756 12.56401 12.53289 12.50478 12.48026 12.45721 12.43327
## [241] 12.40864 12.38353 12.35815 12.33272 12.30742 12.28248 12.25809 12.23448
## [249] 12.21183 12.19037 12.17030 12.15182 12.13419 12.11656 12.09901 12.08161
## [257] 12.06444 12.04758 12.03108 12.01504 11.99953 11.98461 11.97037 11.95687
## [265] 11.94420 11.93243 11.92144 11.91109 11.90137 11.89226 11.88376 11.87587
## [273] 11.86857 11.86187 11.85575 11.85020 11.84522 11.84081 11.83695 11.83363
## [281] 11.83086 11.82862 11.82691 11.82572 11.82504 11.82486 11.82519 11.82600
## [289] 11.82730
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 289)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.56008 12.55878 12.55754 12.55637 12.55526 12.55420 12.55320 12.55224
##   [9] 12.55133 12.55046 12.54962 12.54882 12.54804 12.54729 12.54656 12.54584
##  [17] 12.54514 12.54444 12.54375 12.54306 12.54236 12.54166 12.54095 12.54024
##  [25] 12.53953 12.53882 12.53811 12.53742 12.53674 12.53606 12.53541 12.53477
##  [33] 12.53415 12.53355 12.53298 12.53243 12.53191 12.53143 12.53098 12.53056
##  [41] 12.53018 12.52985 12.52956 12.52931 12.52911 12.52897 12.52887 12.52883
##  [49] 12.52885 12.52893 12.52899 12.52895 12.52883 12.52865 12.52840 12.52810
##  [57] 12.52777 12.52741 12.52704 12.52667 12.52631 12.52597 12.52566 12.52540
##  [65] 12.52519 12.52505 12.52500 12.52503 12.52517 12.52542 12.52579 12.52631
##  [73] 12.52697 12.52780 12.52879 12.52997 12.53135 12.53293 12.53456 12.53608
##  [81] 12.53750 12.53883 12.54009 12.54128 12.54243 12.54354 12.54463 12.54570
##  [89] 12.54678 12.54787 12.54898 12.55014 12.55135 12.55262 12.55397 12.55540
##  [97] 12.55694 12.55860 12.56038 12.56230 12.56437 12.56661 12.56903 12.57163
## [105] 12.57444 12.57747 12.57984 12.58079 12.58048 12.57906 12.57667 12.57348
## [113] 12.56964 12.56529 12.56061 12.55573 12.55081 12.54601 12.54148 12.53738
## [121] 12.53385 12.53105 12.52913 12.52825 12.52857 12.53023 12.53338 12.53819
## [129] 12.54511 12.55429 12.56541 12.57816 12.59224 12.60733 12.62312 12.63930
## [137] 12.65556 12.67159 12.68707 12.70170 12.71516 12.72715 12.74039 12.75745
## [145] 12.77770 12.80056 12.82540 12.85164 12.87866 12.90586 12.93264 12.95839
## [153] 12.98250 13.00438 13.02342 13.03901 13.05447 13.07314 13.09445 13.11781
## [161] 13.14264 13.16839 13.19445 13.22027 13.24526 13.26885 13.29046 13.30951
## [169] 13.32543 13.33764 13.34802 13.35875 13.36974 13.38090 13.39213 13.40335
## [177] 13.41445 13.42536 13.43596 13.44617 13.45589 13.46504 13.47351 13.48122
## [185] 13.48806 13.49396 13.49881 13.50251 13.50499 13.50614 13.50587 13.50408
## [193] 13.50069 13.49560 13.48871 13.47994 13.46863 13.45443 13.43772 13.41884
## [201] 13.39817 13.37605 13.35286 13.32895 13.30468 13.28042 13.25652 13.23334
## [209] 13.21125 13.19061 13.16837 13.14169 13.11126 13.07775 13.04185 13.00424
## [217] 12.96559 12.92658 12.88791 12.85023 12.81424 12.78062 12.75005 12.72320
## [225] 12.69660 12.66676 12.63430 12.59985 12.56405 12.52752 12.49088 12.45478
## [233] 12.41983 12.38667 12.35593 12.32823 12.30421 12.28450 12.26681 12.24858
## [241] 12.23002 12.21134 12.19274 12.17443 12.15663 12.13952 12.12333 12.10826
## [249] 12.09452 12.08231 12.07184 12.06332 12.05596 12.04886 12.04214 12.03587
## [257] 12.03014 12.02505 12.02069 12.01715 12.01451 12.01287 12.01232 12.01295
## [265] 12.01485 12.01811 12.02259 12.02812 12.03467 12.04225 12.05083 12.06042
## [273] 12.07101 12.08259 12.09515 12.10868 12.12317 12.13862 12.15502 12.17236
## [281] 12.19063 12.20983 12.22994 12.25097 12.27289 12.29570 12.31940 12.34398
## [289] 12.36942
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 289)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 11.84002 11.84423 11.84850 11.85281 11.85717 11.86154 11.86591 11.87026
##   [9] 11.87459 11.87887 11.88308 11.88722 11.89126 11.89519 11.89900 11.90266
##  [17] 11.90616 11.90949 11.91262 11.91555 11.91826 11.92072 11.92293 11.92487
##  [25] 11.92652 11.92787 11.92889 11.92958 11.92992 11.92995 11.92974 11.92929
##  [33] 11.92862 11.92774 11.92667 11.92542 11.92401 11.92245 11.92075 11.91893
##  [41] 11.91701 11.91498 11.91288 11.91071 11.90849 11.90622 11.90394 11.90164
##  [49] 11.89934 11.89707 11.89482 11.89261 11.89047 11.88840 11.88641 11.88452
##  [57] 11.88275 11.88067 11.87788 11.87444 11.87040 11.86581 11.86073 11.85521
##  [65] 11.84931 11.84308 11.83658 11.82985 11.82296 11.81595 11.80888 11.80181
##  [73] 11.79478 11.78786 11.78109 11.77454 11.76825 11.76227 11.75667 11.75150
##  [81] 11.74680 11.74264 11.73907 11.73614 11.73391 11.73121 11.72695 11.72126
##  [89] 11.71427 11.70614 11.69698 11.68693 11.67614 11.66474 11.65286 11.64064
##  [97] 11.62822 11.61573 11.60332 11.59110 11.57923 11.56784 11.55706 11.54704
## [105] 11.53790 11.52978 11.52282 11.51716 11.51293 11.51027 11.50931 11.51019
## [113] 11.51305 11.51783 11.52428 11.53229 11.54171 11.55243 11.56432 11.57725
## [121] 11.59109 11.60571 11.62099 11.63679 11.65299 11.66947 11.68609 11.70272
## [129] 11.71924 11.73552 11.75143 11.76684 11.78163 11.79853 11.81994 11.84516
## [137] 11.87354 11.90438 11.93703 11.97080 12.00502 12.03902 12.07213 12.10366
## [145] 12.13294 12.15931 12.18209 12.20390 12.22770 12.25328 12.28043 12.30893
## [153] 12.33858 12.36916 12.40047 12.43229 12.46443 12.49665 12.52877 12.56056
## [161] 12.59181 12.62233 12.65188 12.68028 12.70730 12.73273 12.75637 12.77801
## [169] 12.79743 12.81443 12.83035 12.84663 12.86317 12.87989 12.89667 12.91343
## [177] 12.93008 12.94651 12.96265 12.97838 12.99362 13.00827 13.02224 13.03543
## [185] 13.04776 13.05911 13.06941 13.07855 13.08644 13.09299 13.09811 13.10169
## [193] 13.10364 13.10387 13.10229 13.09880 13.09372 13.08745 13.08002 13.07146
## [201] 13.06180 13.05107 13.03929 13.02650 13.01271 12.99796 12.98229 12.96571
## [209] 12.94825 12.92994 12.90820 12.88096 12.84904 12.81328 12.77449 12.73349
## [217] 12.69113 12.64821 12.60556 12.56401 12.52438 12.48749 12.45418 12.42526
## [225] 12.39617 12.36238 12.32476 12.28418 12.24153 12.19767 12.15349 12.10984
## [233] 12.06762 12.02768 11.99091 11.95818 11.93037 11.90834 11.88913 11.86933
## [241] 11.84914 11.82880 11.80850 11.78848 11.76895 11.75012 11.73222 11.71545
## [249] 11.70005 11.68621 11.67417 11.66413 11.65521 11.64643 11.63791 11.62976
## [257] 11.62211 11.61505 11.60872 11.60321 11.59865 11.59515 11.59282 11.59177
## [265] 11.59213 11.59400 11.59724 11.60160 11.60709 11.61368 11.62138 11.63016
## [273] 11.64004 11.65098 11.66299 11.67605 11.69016 11.70531 11.72149 11.73868
## [281] 11.75689 11.77609 11.79629 11.81747 11.83962 11.86274 11.88681 11.91183
## [289] 11.93779
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")